1 ========================================================================
2 SILVERLIGHT APPLICATION : CSSL4FileDragDrop Project Overview
3 ========================================================================
5 /////////////////////////////////////////////////////////////////////////////
8 Drag&Drop is a newly supported feature in Silverlight4. In this sample, we demonstrate
9 how to implement visual element's drag&drop function and how to access local files
10 by using file drag&drop feature.
13 /////////////////////////////////////////////////////////////////////////////
17 1. Open CSSL4FileDragDrop solution and rebuild. (Due to an known issue, the drag&drop function are not allowed when running Silverlight app in Admin
18 mode IE browser. Therefore, user need avoid running this Sample in VisualStudio or IE with
20 2. Press Ctrl+F5 to open the test page.
21 3. In opened page, you may find a Silverlight application.
22 a. Try drag any image files from your windows explorer to Silverlight application
23 blue region. You may find that the image files will be loaded and displayed in
24 Silverlight application. (Note that: Silverlight only support .jpg,.png image format)
25 b. Try drag and drop the image icon.
28 /////////////////////////////////////////////////////////////////////////////
31 Silverlight 4 Tools for Visual Studio 2010
32 http://www.silverlight.net/getstarted/
35 http://www.silverlight.net/getstarted/
38 /////////////////////////////////////////////////////////////////////////////
41 1. How to drag local files to Silverlight application?
42 First, enable the file drop function on elements by setting UIElement.AllowDrop to true.
43 Then, handle UIElement.Drop event. To access the draged files, we may use the code below:
45 private void LayoutRoot_Drop(object sender, DragEventArgs e)
50 var files = e.Data.GetData(DataFormats.FileDrop) as FileInfo[];
51 foreach (var file in files)
58 2. How to implement UI element drag and drop function?
59 First, register UIElement.MouseLeftButtonDown event, in eventhandler, set
60 "isdrag" flag to true, and store the mouse position relative to the drag
61 target element, and then capture mouse by UIElement.CaptureMouse() method.
63 element.MouseLeftButtonDown += (s, e) =>
66 element.CaptureMouse();
67 relativepos = e.GetPosition(element);
70 Second, register UIElement.MouseMove event, in eventhandler, check "isdrag"
71 flag value, if is true, set element's postion to
72 [mouse current position relative to element's container] - [stored mouse position relative to element]
74 element.MouseMove += (s, e) =>
78 var pos = e.GetPosition(LayoutRoot);
79 Canvas.SetLeft(element, pos.X - relativepos.X);
80 Canvas.SetTop(element, pos.Y - relativepos.Y);
84 Third, register UIElement.MouseLeftButtonUp event, in eventhandler, set
85 "isdrag" to false, and release mouse capture.
87 element.MouseLeftButtonUp += (s, e) =>
89 element.ReleaseMouseCapture();
93 /////////////////////////////////////////////////////////////////////////////
97 http://msdn.microsoft.com/en-us/library/system.windows.uielement.drop(VS.95).aspx
99 /////////////////////////////////////////////////////////////////////////////